<template>
  <div>
    <el-card>
      <div class="list-h">
        <i></i>
        <el-form
          :inline="true"
          :model="formInline"
          ref="formRef"
          @submit.prevent
          class="demo-form-inline"
        >
          <el-form-item label="关键字" prop="businessKeywordLike">
            <el-input
              class="width-custom120"
              v-model="formInline.businessKeywordLike"
              clearable
            >
            </el-input>
          </el-form-item>
          <el-form-item label="项目名称" prop="projectNameLike">
            <el-input
              class="width-custom120"
              v-model="formInline.projectNameLike"
              clearable
            >
            </el-input>
          </el-form-item>
          <!-- <el-form-item label="业务类型" prop="flowType">
            <el-select
              clearable
              class="width-custom120"
              v-model="formInline.flowType"
            >
              <el-option
                v-for="item in downloadTypeList"
                :label="item.dicValue"
                :key="item.dicCode"
                :value="item.dicCode"
              />
            </el-select>
          </el-form-item> -->

          <el-form-item label="创建人" prop="createUserNameLike">
            <el-input
              class="width-custom120"
              v-model="formInline.createUserNameLike"
              clearable
            >
            </el-input>
          </el-form-item>

          <el-form-item label="创建时间">
            <el-date-picker
              clearable
              class="width-custom220"
              v-model="timeValue"
              type="daterange"
              range-separator="-"
              start-placeholder="开始时间"
              end-placeholder="结束时间"
              value-format="YYYY-MM-DD hh:mm:ss"
              @change="handleChangeDate"
            />
          </el-form-item>

          <el-form-item>
            <el-button @click="handleReset">重置</el-button>
            <el-button type="primary" @click="handleSearch">查询</el-button>
          </el-form-item>
        </el-form>
      </div>
      <!-- <div class="header-btn">
        <el-button
          type="primary"
          :disabled="!multipleSelection.length"
          @click="handleDownload"
          >下载</el-button
        >
      </div> -->
      <!-- <el-skeleton style="height: 50vh" :rows="8" :loading="loading" animated> -->
      <el-table
        :data="tableData"
        border
        style="width: 100%"
        v-loading="loading"
        :header-cell-style="{ background: '#eef1f6', color: '#606266' }"
      >
        <template #empty>
          <el-empty :image-size="150" description="暂无数据"></el-empty>
        </template>
        <!-- <el-table-column
          type="selection"
          :reserve-selection="true"
          width="50"
        ></el-table-column> -->
        <el-table-column type="index" fixed="left" label="序号">
          <template v-slot="scope">
            <span>{{
              (pageState.pageNum - 1) * pageState.pageSize + scope.$index + 1
            }}</span>
          </template>
        </el-table-column>
        <el-table-column prop="projectCode" label="项目编号"></el-table-column>
        <el-table-column
          prop="projectName"
          label="项目名称"
          show-overflow-tooltip
        ></el-table-column>
        <el-table-column
          prop="projectTypeName"
          label="项目类型"
        ></el-table-column>
        <el-table-column prop="createUserName" label="创建人"></el-table-column>
        <el-table-column
          prop="createTime"
          label="创建时间"
          :min-width="120"
        ></el-table-column>
        <el-table-column prop="" label="审批状态">
          <template #default="{ row }">
            {{ formatterStatusType(row) }}
          </template>
        </el-table-column>
        <el-table-column label="操作" width="100" fixed="right">
          <template #default="{ row }">
            <el-button type="text" @click="handleLook(row)">查看</el-button>
          </template>
        </el-table-column>
      </el-table>
      <re-pagination
        v-if="total > 0"
        :total="total"
        v-model:page="pageState.pageNum"
        v-model:limit="pageState.pageSize"
        @pagination="handleChangePage"
      />
    </el-card>
  </div>
</template>

<script lang="ts" setup>
import { ref, reactive, onMounted, unref } from "vue";
import { useRoute, useRouter } from "vue-router";

// import RePagination from "/@/components/RePagination/index.vue";
import * as api from "/@/api/project";
// import { postDocumentDownload } from "/@/utils/download";
import { ProjectStatusMap } from "/@/views/project/types";

const route = useRoute();
const router = useRouter();

const initData = {
  projectNameLike: "", //
  businessKeywordLike: "",
  createUserNameLike: "",
  createTimeStrStart: "", //
  createTimeStrEnd: "" //
};

let timeValue = ref(null);

const formInline = reactive(initData);
const pageState = reactive({
  pageNum: 1,
  pageSize: 10
});

const formRef = ref<HTMLFormElement | null>(null) as any;

const tableData = ref([]);
// let downloadTypeList = ref([]);
// let flowTypeMap = new Map([]);

let loading = ref<boolean>(false);
let total = ref<number>(0);
const formParams = ref<typeof initData>({ ...initData });

// const handleDownload = async () => {
//   postDocumentDownload(multipleSelection.value);
// };

const getTableList = async (obj: object = {}) => {
  try {
    loading.value = true;
    const { code, data } = await api.postProjectSearch({
      ...obj,
      ...pageState
    });
    if (code === 200) {
      tableData.value = data.list;
      total.value = data.rows;
    }
  } catch (err) {
    console.log(err);
  } finally {
    loading.value = false;
  }
};

const handleChangeDate = item => {
  if (item) {
    formInline.createTimeStrStart = item[0];
    formInline.createTimeStrEnd = item[1];
  } else {
    formInline.createTimeStrStart = "";
    formInline.createTimeStrEnd = "";
  }
};

const handleReset = () => {
  formRef.value.resetFields();
  timeValue.value = null;
  handleChangeDate(null);
  formParams.value = initData;
  getTableList(formInline);
};

const handleSearch = () => {
  pageState.pageNum = 1;
  formParams.value = formInline;
  getTableList(formInline);
};

const handleChangePage = () => {
  getTableList(formParams.value);
};

const handleLook = row => {
  console.log(row);
  if (row.saveStatus === "1") {
    router.push({
      path: "/project/create",
      query: { id: row.id, mode: "edit" }
    });
  } else {
    router.push({
      path: "/project/detail",
      query: { id: row.id, mode: "edit" }
    });
  }
};

// 文档类型
// const initDoucumentTypeList = async () => {
//   try {
//     const params = {
//       dicType: "doc_download_type"
//     };
//     const { code, data } = await postDictList(params);
//     if (code === 200) {
//       downloadTypeList.value = data;
//       data.forEach(item => {
//         flowTypeMap.set(item.dicCode, item.dicValue);
//       });
//     }
//   } catch (err) {
//     console.log(err);
//   }
// };

const formatterStatusType = row => {
  if (!row.approvalStatus) return "-";
  return ProjectStatusMap.get(row.approvalStatus);
};

onMounted(() => {
  const { query } = unref(route);
  formInline.businessKeywordLike = (query?.name as string) ?? "";
  getTableList(formInline);
});

// getProjectList();
// initDoucumentTypeList();
</script>
<style scoped lang="scss">
.card-box {
  margin-bottom: 10px;
}
.header-btn {
  margin: 10px 0;
  display: flex;
  justify-content: flex-end;
  align-items: center;
}
</style>
